/*
MIT License
Copyright (c) 2025 Chuck Fu <changlin.fu@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef CF_CRYPTO_SM3_H
#define CF_CRYPTO_SM3_H
#include <stdint.h>

// -----------------------------------------------------------------------------
// SM3 Sync Call Interfaces
// -----------------------------------------------------------------------------
/**
 * SM3 Cryptographic Hash Algorithm.
 * Map any length bit stream to a 256 bits hash value.
 *
 * @param hash (output) the 256 bits (32 bytes) hash value
 * @param message_ptr  pointer to the bit string
 * @param message_bits active bit count in the byte stream
 */
void sm3_hash(
    uint8_t hash[32],
    const uint8_t *message_ptr,
    uint64_t message_bits
);

// -----------------------------------------------------------------------------
// SM3 Streaming Call Interfaces
// -----------------------------------------------------------------------------
/**
 * SM3 Running Context.
 * Used to keep internal state in streaming mode.
 */
typedef struct sm3_ctx_st {
    uint8_t  hash[32];      // hash result 256 bits
    uint8_t  segment[64];   // last segment received from the [Update] call
    uint32_t segment_bits;  // last segment size in bit (max 512 bits)
    uint64_t total_bits;    // total bits proceed
} sm3_ctx_t;

/**
 * SM3 Streaming [Start] Call
 * Initialize the Running Context.
 * @param ctx the SM3 context storing the needed information to run SM3
 *            in the Stream mode.
 */
void sm3_stream_start(sm3_ctx_t *ctx);

/**
 * SM3 Streaming [Update] Call
 * Process message stream segment (MAX 512 active bits).
 * (Notice) Caller needs to make sure the inactive bits are set to 0.
 *
 * @param ctx the SM3 context used.
 * @param segment point to the message segment
 * @param segment_bits the segment size in bit
 *
 */
void sm3_stream_update(
    sm3_ctx_t *ctx,
    const uint8_t segment[],
    uint32_t segment_bits
);

/**
 * SM3 Streaming [Finish] Call
 * Populate the final hash result.
 *
 * @param ctx the SM3 context used.
 * @param hash (output) the 256 bits (32 bytes) hash value
 */
void sm3_stream_finish(sm3_ctx_t *ctx, uint8_t hash[32]);


#endif //CF_CRYPTO_SM3_H
